package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.vo.CourseInfoVo;
import com.atguigu.serviceedu.entity.vo.CoursePublishVo;
import com.atguigu.serviceedu.entity.vo.CourseQuery;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
//@CrossOrigin
@RestController
@RequestMapping("/serviceedu/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    //条件分页查询
    @PostMapping("selectCoursePage/{page}/{limit}")
    public R selectCoursePage(
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit,
            @RequestBody(required = false) CourseQuery courseQuery){

        Page<EduCourse> coursepage = new Page<>(page,limit);

        //取出条件
        String teacherId = courseQuery.getTeacherId();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(teacherId)){
            wrapper.eq("teacher_id",teacherId);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            if("1".equals(status)){
                wrapper.eq("status","Normal");
            }else {
                wrapper.eq("status","Draft");
            }

        }

        wrapper.orderByDesc("gmt_modified");

        eduCourseService.page(coursepage,wrapper);
        long total = coursepage.getTotal();

        List<EduCourse> courseList = coursepage.getRecords();

        return R.ok().data("total",total).data("courseList",courseList);
    }

    @PostMapping("addCourse")
    public R addCourse(@RequestBody CourseInfoVo courseInfoVo){
        String id = eduCourseService.saveCourse(courseInfoVo);
        return R.ok().data("id",id);
    }


    //根据课程id查询课程信息
    @GetMapping("findCourseById/{courseId}")
    public R findCourseById(@PathVariable String courseId){

        CourseInfoVo courseInfoVo = eduCourseService.findCourseById(courseId);

        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R setCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        eduCourseService.updateCourseInfo(courseInfoVo);

        return R.ok();
    }

    //查询最终发布的信息
    @GetMapping("getCoursePublishVo/{courseId}")
    public R getCoursePublishVo(@PathVariable String courseId){
       CoursePublishVo coursePublishVo =  eduCourseService.getCoursePublishVo(courseId);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }

    //课程最终发布，修改课程状态
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable("courseId") String courseId){
        EduCourse course = new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");
        eduCourseService.updateById(course);
        return R.ok();
    }


    //删除课程：包括课程的小节，章节，描述，本身
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){

        eduCourseService.removeCourseById(courseId);
        return R.ok();
    }
}

